import java.util.Map;

public class SQLformatter {
	static String path_surround(String value) {
		return "`" + value + "`";
	}

	static String path_surround(String[] pathNodes) {
		StringBuilder stringBuffer = new StringBuilder(" ");
		boolean first = true;
		for (String pathNode : pathNodes) {
			if (first) first = false;
			else stringBuffer.append(".");
			stringBuffer.append(path_surround(pathNode));
		}
		return new String(stringBuffer.append(' '));
	}
	static String titleAppend(String[] titles){
		StringBuilder stringBuffer = new StringBuilder(" ( ");
		boolean first = true;
		for (String title : titles) {
			if (first) first = false;
			else stringBuffer.append(",");
			stringBuffer.append(path_surround(title));
		}
		return new String(stringBuffer.append(" ) "));
	}

	static String pathCollection(String path) {
		return "(" + path_surround(path) + ")";
	}

	static String pathCollection(String[] pathNodes) {
		return " (" + path_surround(pathNodes) + ") ";
	}

	static String value_quote(String value) {
		return " '" + value + "' ";
	}

	static String value_quote(String[] values){
		StringBuilder stringBuilder=new StringBuilder();
		boolean first=true;
		for(String value:values){
			if(first)first=false;
			else stringBuilder.append(" , ");
			stringBuilder.append(value_quote(value));
		}
		return stringBuilder.toString();
	}
	static String parentheses(String content){
		return " ( "+content+" ) ";
	}

	static String sqlUrl(int port) {
		return "jdbc:MySQL://127.0.0.1:" + port;
	}
	static String sqlUrl(int port, String schemaName) {
		return sqlUrl(port) + "/" + schemaName;
	}
	static String sqlUrl(int port, String schemaName, Map<String,String> additionalParams){
		StringBuilder stringBuilder=new StringBuilder(sqlUrl(port,schemaName));
		boolean first=true;
		for(String additionalParam:additionalParams.keySet()){
			if(first){	stringBuilder.append('?');	first=false;	}
			else 		stringBuilder.append('&');
			stringBuilder.append(additionalParam).append('=').
					append(additionalParams.get(additionalParam));
		}
		return stringBuilder.toString();
	}
}
